package sz.szsmk.citizencard.ui.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chenenyu.router.annotation.Route;
import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.SearchListActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.SearchViewResponse;
import sz.szsmk.citizencard.mvp.contract.ISearchListContact;
import sz.szsmk.citizencard.mvp.presenter.SearchListPresenter;
import sz.szsmk.citizencard.ui.adapter.SearchViewAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/16 20:32
 * description:
 */
@Route("searchlist")
public class SearchListActivity extends IBaseActivity<SearchListPresenter, SearchListActivityBinding>
        implements ISearchListContact.ISearchListView, View.OnClickListener {


    private String keyword;
    private String value;
    private RecyclerView searchListRv;
    private SearchViewAdapter searchViewAdapter;
    private List<SearchViewResponse.SpotDeatilSpotListBean> spot_deatil_spot_list;
    private String door;
    private SearchViewResponse.SpotDeatilHotelListBean spotDeatilHotelListBean;
    private List<SearchViewResponse.SpotDeatilHotelListBean> spotDeatilHotelListBeans;
    private List<SearchViewResponse.SpotDeatilHotelListBean> spot_deatil_hotel_list;

    @Override
    protected int attachLayoutRes() {
        return R.layout.search_list_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        keyword = getIntent().getStringExtra("key");
        value = getIntent().getStringExtra("value");
        if (value.equals("familycard")) {
            dataBinding.searchView.setVisibility(View.GONE);
        } else {
            dataBinding.searchView.setVisibility(View.VISIBLE);
        }
        dataBinding.titleLayout.settingTitle.setText("搜索");
        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.searchJindian.setOnClickListener(this);

        searchListRv = dataBinding.searchListRv;

        searchViewAdapter = new SearchViewAdapter(this);
        searchListRv.setLayoutManager(new LinearLayoutManager(this));
        searchListRv.setAdapter(searchViewAdapter);

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getSearchList(keyword, value);
            }
        });
        dataBinding.swipeRefreshLayout.setEnableLoadMore(false);

        dataBinding.swipeRefreshLayout.autoRefresh();
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void getSearchListSucc(SearchViewResponse response) {
        spot_deatil_spot_list = response.getSpot_deatil_spot_list();
        spot_deatil_hotel_list = response.getSpot_deatil_hotel_list();
        if (spot_deatil_spot_list != null) {
            spotDeatilHotelListBeans = new ArrayList<>();
            for (int i = 0; i < spot_deatil_spot_list.size(); i++) {
                SearchViewResponse.SpotDeatilSpotListBean spotDeatilSpotListBean = spot_deatil_spot_list.get(i);
                String s = new Gson().toJson(spotDeatilSpotListBean);
                spotDeatilHotelListBean = new Gson().fromJson(s, SearchViewResponse.SpotDeatilHotelListBean.class);
                spotDeatilHotelListBeans.add(spotDeatilHotelListBean);
            }
        }
        searchViewAdapter.setHotelData(spotDeatilHotelListBeans);
        searchViewAdapter.notifyDataSetChanged();
        boolean hasMore = spot_deatil_spot_list.size() == 15;
        if(hasMore){

        }else{
            //完成加载并标记没有更多数据
            dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    @Override
    public void getSearchListFail() {
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.search_jindian) {
            dataBinding.jingdianLine.setVisibility(View.VISIBLE);

            dataBinding.searchJindian.setTextColor(getResources().getColor(R.color.color_00AAFF));
            searchViewAdapter.setHotelData(spotDeatilHotelListBeans);
            searchViewAdapter.notifyDataSetChanged();
            boolean hasMores = spot_deatil_spot_list.size() == 15;
            if (hasMores) {

            } else {
                //完成加载并标记没有更多数据
                dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

}
